{include file='public/header' /}
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body table-tool-mini full-table">
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">商品标题：</label>
                        <div class="layui-input-inline mr0">
                            <input name="title" id="title" class="layui-input" type="text" placeholder="输入商品标题"/>
                        </div>
                    </div>
                    <div class="layui-inline" style="padding-right: 110px;">
                        <button class="layui-btn icon-btn" id="searchBtn" lay-filter="formSubSearchUser" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                        <button id="btnAddUser" class="layui-btn icon-btn {:auth('mall.integral_item/add')}" ><i class="layui-icon">&#xe654;</i>添加</button>
                    </div>
                </div>
            </div>

            <table id="tableUser" lay-filter="tableUser"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="options">
    <a class="layui-btn layui-btn-sm layui-btn-xs {:auth('mall.integral_item/get_edit')}" lay-event="edit">商品详情</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs {:auth('mall.integral_item/delete')}" lay-event="del">删除</a>
</script>
<!-- 表格状态列 -->
<script type="text/html" id="thumb">
    <a href="{{d.thumb}}" target="_blank" title="点击查看">
        <img src="{{d.thumb}}" alt="" width="28" height="28">
    </a>
</script>
<!-- 表单弹窗 -->


<!-- js部分 -->
{include file='public/footer' /}
<!--<script type="text/javascript" src="/static/public/assets/js/common.js?v=315"></script>-->
<script>
    layui.use(['layer', 'table', 'admin','form'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var admin = layui.admin;
        var table = layui.table;
        //用户表格初始化
        var dataTable =
                table.render({
                    elem: '#tableUser'
                    , autoSort: false
                    , height: 500
                    , url: "{:__url('mall.integral_item/getData')}" //数据接口
                    , page: true //开启分页
                    , cols: [[ //表头
                        {checkbox: true, fixed: true}
                        , {field: 'id', title: 'ID', sort: true, width: 80}

                        , {field: 'title', title: '标题'}
                        , {field: 'thumb', title: '缩略图', toolbar: '#thumb', width: 100}

                        , {field: 'create_time', title: '创建时间'}
                        , {field: 'update_time', title: '更新时间'}
                        , {fixed: 'right', width: 140, align: 'center', toolbar: '#options'}
                    ]]
                });


        //监听工具条
        table.on('tool(tableUser)', function (obj) { //注：tool是工具条事件名，dataTable是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                    , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'del') {
                layer.confirm('确认删除吗？', function (index) {
                    layer.close(index);
                    var load = layer.load();
                    $.post("delete", {
                        _method: 'delete',
                        id: [data.id]
                    }, function (res) {
                        layer.close(load);
                        if (res.code == 1) {
                            layer.msg(res.msg, {icon: 1}, function () {
                                obj.del();
                            },1000)
                        } else {
                            layer.msg(res.msg, {icon: 2})
                        }
                    });
                });
            } else if (layEvent === 'edit') {
                showEditModel('编辑商品','/mall.integral_item/get_edit?id='+data.id );

            }
        });

        //按钮批量删除
        $("#listDelete").click(function () {
            var ids = [];
            var hasCheck = table.checkStatus('tableUser');
            var hasCheckData = hasCheck.data;
            if (hasCheckData.length > 0) {
                $.each(hasCheckData, function (index, element) {
                    ids.push(element.id)
                })
            }
            if (ids.length > 0) {
                layer.confirm('确认删除吗？', function (index) {
                    layer.close(index);
                    var load = layer.load();
                    $.post("{{ route('mall.integral_item.delete') }}", {
                        _method: 'delete',
                        ids: ids
                    }, function (res) {
                        layer.close(load);
                        if (res.code == 0) {
                            layer.msg(res.msg, {icon: 1,time:1000}, function () {
                                dataTable.reload({page: {curr: 1}});

                            })
                        } else {
                            layer.msg(res.msg, {icon: 2,time:1000})
                        }
                    });
                });
            } else {
                layer.msg('请选择删除项', {icon: 2,time:1000})
            }
        })

        // 添加按钮点击事件
        $('#btnAddUser').click(function () {
            showEditModel('新增商品',"add");
        });

        // 显示表单弹窗
        function showEditModel(title,url) {
            layer.full(admin.open({
                type: 2,

                title: title,
                content: url,
                btnAlign: 'c',   // 按钮居中
                success: function (layero, dIndex) {
                    form.render();    // 表单渲染
                },

                yes: function (index, layero) {

                    // 获取弹出层中的form表单元素
                    var formSubmit=layer.getChildFrame('form', index);
                    //获取表单数据
                    var data = {dosubmit:1};
                    var action = formSubmit[0]['action'];
                    var a = formSubmit.serializeArray();
                    $.each(a, function () {
                        if (data[this.name] !== undefined) {
                            if (!data[this.name].push) {
                                data[this.name] = [data[this.name]];
                            }
                            data[this.name].push(this.value || '');
                        } else {
                            data[this.name] = this.value || '';
                        }
                    });

                    if (data.parent_id == '') {
                        data.parent_id = '0';
                    }

                    layer.load(2);
                    $.post(action, data, function (res) {
                        layer.closeAll('loading');
                        if (res.code == 0) {
                            layer.msg(res.msg, {icon: 1});
                            layer.close(index);

                            dataTable.reload({page: {curr: 1}});

                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'json');

                    return false;

                },
                btn2: function (index, layero) {
                    layer.close(index);
                }


            }));
        }



        //搜索
        $("#searchBtn").click(function () {
            var catId = $("#title").val();
            dataTable.reload({
                where: {title: catId},
                page: {curr: 1}
            })
        })
    })
</script>

</body>
</html>
